<?php
class IndexApp extends CommonApp {
   
    public function indexAct(){
   //  	if($_GET){
   //  		$icon_hex = "";
			// $search = array(" ","　","\n","\r","\t");
			// $replace = array("","","","","");
			// $icon_hexs = str_replace($search, $replace, $icon_hex);
	  //   	$imgdata = pack('H*',$icon_hexs);
	  //   	$savePath = PUB_DIR.'/share/images/uploads/'; // 设置附件上传（子）目录
	  //   	if(!file_exists($savePath))
	  //   		//检查是否有该文件夹，如果没有就创建，并给予最高权限
			// 	mkdir($savePath, 0700);
			// $image_name = time().'.png';
			// $new_file = $savePath.$image_name;
			// //$new_file = $savePath.time();
			// // var_dump($_GET);
			// // $data = $_GET;
			// if (file_put_contents($new_file, $imgdata)){
			// 	$data['image'] = '/share/images/uploads/'.$image_name;
			// 	$ok = DB('datas')->clear()->add($data);
			// 	if($ok)
			// 		echo '新文件保存成功：', $new_file;
			// }else{
			// 	echo '新文件保存失败';
			// }	
   //  	}
    	$this->display();
    }
   

    public function switch1Act(){
        // ca:01:se  开
        // ca:10:se  关
        // ca:11:se  硬件操作成功
        // ca:00:se  硬件操作成功
        if(isset($_GET['uid']) && isset($_GET['switch'])){
            $data['uid'] = $_GET['uid'];//客户端ip
            $data['switch'] = $_GET['switch'];//开关
            $ok = DB("connect_info")->where('uid ="'.$data['uid'].'"')->find();
            if($ok >= '1'){
                $ok2 = DB("datas")->where('uid ="'.$data['uid'].'" AND switch = "'.$data['switch'].'"')->find();
                if(!$ok2){
                    //  建立socket连接到内部推送端口 
                    $client = stream_socket_client('tcp://112.74.84.200:2001',$errno, $errmsg,1);
                    if($client){
                    	//  推送的数据，包含uid字段，表示是给这个uid推送 
	                    $value['uid'] =$_GET['uid'];
	                    $value['value'] = 'c'.$data['switch'].'a';
	                    fwrite($client, My::encodeJson($value)."\n");
	                    //  读取返回结果
	                    $result = fread($client,8192);
	                    $result = (string)trim($result);
	                    // $result = substr($result,3,2);
	                    if($result == '1'){
	                       $ok3 = DB("datas")->clear()->where("uid = '".$data['uid']."'")->save($data);
	                       if($ok3) echo My::encodeJson(array('status' => '1', 'message'=>'硬件设备已更改！'));
	                       else echo My::encodeJson(array('status' => '0', 'message'=>'硬件设备数据储存出错！'));
	                    }else if($result == '0') echo My::encodeJson(array('status' => '0', 'message'=>'硬件设备更改失败！'));
	                    else if($result == '2') echo My::encodeJson(array('status' => '0', 'message'=>'硬件未开启！'));
	                    else echo My::encodeJson(array('status' => '0', 'message'=>'其他未知错误！'));
                    }else echo My::encodeJson(array('status' => '0', 'message'=>'远程服务未开启！'));
                }else echo My::encodeJson(array('status' => '0', 'message'=>'请忽进行重复操作！'));
            }else echo My::encodeJson(array('status' => '0', 'message'=>'该硬件已断开连接或者没有该uid的硬件设备！'));
        }else echo My::encodeJson(array('status' => '0', 'message'=>'没有参数传入！'));
        

    }
    public function automaticAct(){
        //12关，13开
        if(isset($_GET['uid']) && isset($_GET['switch'])){
            $data['uid'] = $_GET['uid'];//客户端uid
            $data['automatic'] = $_GET['switch'];//开关
            $ok = DB("connect_info")->where('uid ="'.$data['uid'].'"')->find();
            if($ok >= '1'){
                $ok2 = DB("datas")->where('uid ="'.$data['uid'].'" AND automatic = "'.$data['automatic'].'"')->find();
                if(!$ok2){
                    //  建立socket连接到内部推送端口 
                    $client = stream_socket_client('tcp://112.74.84.200:2001',$errno, $errmsg,1);
                    if($client){
                    	//  推送的数据，包含uid字段，表示是给这个uid推送 
	                    $value['uid'] =$_GET['uid'];
	                    $value['value'] = '{'.$data['automatic'].'}';
	                    fwrite($client, My::encodeJson($value)."\n");
	                    //  读取返回结果
	                    $result = fread($client,8192);
	                    $result = (string)trim($result);
	                    // $result = substr($result,3,2);
	                    if($result == '1'){
	                       $ok3 = DB("datas")->clear()->where("uid = '".$data['uid']."'")->save($data);
	                       if($ok3) echo My::encodeJson(array('status' => '1', 'message'=>'硬件设备已更改！'));
	                       else echo My::encodeJson(array('status' => '0', 'message'=>'硬件设备数据储存出错！'));
	                    }else if($result == '0') echo My::encodeJson(array('status' => '0', 'message'=>'硬件设备更改失败！'));
	                    else if($result == '2') echo My::encodeJson(array('status' => '0', 'message'=>'硬件未开启！'));
	                    else echo My::encodeJson(array('status' => '0', 'message'=>'其他未知错误！'));
                    }else echo My::encodeJson(array('status' => '0', 'message'=>'远程服务未开启！'));
                }else echo My::encodeJson(array('status' => '0', 'message'=>'请忽进行重复操作！'));
            }else echo My::encodeJson(array('status' => '0', 'message'=>'该硬件已断开连接或者没有该uid的硬件设备！'));
        }else echo My::encodeJson(array('status' => '0', 'message'=>'没有参数传入！'));
        
    }
}